package GreedyThought;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

// Lettcode 406: https://leetcode-cn.com/problems/queue-reconstruction-by-height/
public class reconstructQueue {
    public int[][] reconstructQueue(int[][] people) {
        if (people == null ||  people.length == 0 || people[0].length == 0) {
            return new int[0][0];
        }
        Arrays.sort(people, new Comparator<int[]>() {
            @Override
            public int compare(int[] ints, int[] t1) {
                if (ints[0] == t1[0]) {
                    return ints[1] - t1[1];
                } else {
                    return t1[0] - ints[0];
                }
            }
        });
        List<int[]> queue = new ArrayList<>();
        for (int[] p: people) {
            queue.add(p[1], p);
        }
        return queue.toArray(new int[queue.size()][]);
    }
}
